package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.BaseOnclickListener;
import com.example.myapplication.R;

import com.example.myapplication.entity.Artists;
import com.example.myapplication.entity.SearchResult;
import com.example.myapplication.entity.SearchSinger;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class ResultSingerAdapter extends RecyclerView.Adapter<ResultSingerAdapter.ResultSingerViewHolder> {
    private SearchResult result;
    private List<SearchResult.ArtistsBeanX> artists;
    private SearchResult.ArtistsBeanX artistsBeanX;
    private BaseOnclickListener onclickListener;
    private Context context;

    public ResultSingerAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<SearchResult.ArtistsBeanX> artists){
        this.artists = artists;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ResultSingerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ResultSingerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_result_singer, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ResultSingerViewHolder holder, int position) {
        artistsBeanX = artists.get(position);

        Glide.with(context)
                .load(artistsBeanX.getPicUrl())
                .into(holder.img);
        holder.tvName.setText(artistsBeanX.getName());

        if (!artistsBeanX.isFollowed()){
            holder.tvMark.setText("关注");
        }

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onclickListener != null){
                    onclickListener.click(position);
                }
            }
        });


        holder.tvMark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }


    @Override
    public int getItemCount() {
        return artists == null ? 0 :artists.size();
    }

    class ResultSingerViewHolder extends RecyclerView.ViewHolder{
        CircleImageView img;
        TextView tvName;
        TextView tvMark;
        View view;

        public ResultSingerViewHolder(@NonNull View itemView) {
            super(itemView);

            img = itemView.findViewById(R.id.circle_img);
            tvName = itemView.findViewById(R.id.tv_singer_name);
            tvMark = itemView.findViewById(R.id.mark);
            view = itemView.findViewById(R.id.view_layout);
        }
    }

    public void setBaseOnclickListener(BaseOnclickListener onclickListener){
        this.onclickListener = onclickListener;
    }
}
